package com.eianiao.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eianiao.bean.Orders;
import com.eianiao.common.PageParam;
import com.eianiao.common.R;
import com.eianiao.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 提交订单！
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Orders orders){

        int row = orderService.add(orders);
        if(row > 0){
            return R.success("提交订单成功！");
        }else{
            return R.error("提交订单失败！");
        }
    }

    @GetMapping("/userPage")
    public R findByPage(PageParam pageParam){
        try {
            IPage<Orders> iPageList = orderService.findByPage(pageParam);
            return R.success(iPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询订单失败");
        }
    }

    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        System.out.println("pageParam.getNumber() = " + pageParam.getNumber());
        System.out.println("-----------------------------------------------");
        System.out.println("pageParam.getBeginTime() = " + pageParam.getBeginTime());
        System.out.println("-----------------------------------------------");
        System.out.println("pageParam.getEndTime() = " + pageParam.getEndTime());
        System.out.println("-----------------------------------------------");
        try {
            IPage<Orders> iPageList = orderService.findByPage(pageParam);

            return R.success(iPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询订单失败");
        }
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R update(@RequestBody Orders orders){
        int row = orderService.update(orders);
        if (row>0){
            return R.success("修改订单状态成功！");
        }
            return R.error("修改订单状态失败！");

    }


}
